package sink;

import bean.SensorReading;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import source.Source4_UDF;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;


/**
 * @Description: TODO QQ1667847363
 * @author: xiao kun tai
 * @date:2021/11/3 17:17
 */
public class Sink4_JDBC {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);


        String inputPath = "src/main/resources/sensor.txt";

        DataStream<String> inputStream = env.readTextFile(inputPath);

        DataStream<SensorReading> dataStream = inputStream.map(line -> {
            String[] fields = line.split(",");
            return new SensorReading(fields[0], new Long(fields[1]), new Double(fields[2]));
        });


        DataStreamSource<SensorReading> sensorReadingStream = env.addSource(new Source4_UDF.MySensorSource());

        /**
         * 定义es的连接配置mysql JDBC
         */
        dataStream.addSink(new MyJdbcSink());
//        sensorReadingStream.addSink(new MyJdbcSink());
        env.execute();


    }

    /**
     * 实现自定义的SinkFunction
     */

    public static class MyJdbcSink extends RichSinkFunction<SensorReading> {
        /**
         * 声明连接和预编译域集
         */
        Connection connection = null;
        PreparedStatement insertStmt = null;
        PreparedStatement updateStmt = null;

        @Override
        public void open(Configuration parameters) throws Exception {
            connection = DriverManager.getConnection("jdbc:mysql://192.168.88.106/flink?characterEncoding=utf8&amp;useSSL=false",
                    "root", "000000");
            insertStmt = connection.prepareStatement(
                    "insert into sensor_temp (id,temp) values (?,?)");
            updateStmt = connection.prepareStatement(
                    "update sensor_temp set temp = ?  where id = ? ");
        }

        /**
         * 每来一条数据，调用连接，执行sql
         */
        @Override
        public void invoke(SensorReading value, Context context) throws Exception {
            /**
             * 没有更新直接插入
             */
            updateStmt.setDouble(1, value.getTemperature());
            updateStmt.setString(2, value.getId());
            updateStmt.execute();
            if (updateStmt.getUpdateCount() == 0) {
                insertStmt.setString(1, value.getId());
                insertStmt.setDouble(2, value.getTemperature());
                insertStmt.execute();
            }

        }

        @Override
        public void close() throws Exception {
            insertStmt.close();
            updateStmt.close();
            connection.close();
        }
    }


}
